gh-143658: Use str.lower and replace to further improve performance of importlib.metadata.Prepared.normalized#144083
Conversation
….metadata.Prepared.normalized Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
str.lower and replace to further improve performance of importlib.metadata.Prepared.normalizedstr.lower and replace to further improve performance of importlib.metadata.Prepared.normalized
|
This |
|
…formance of `importlib.metadata.Prepared.normalized` (python#144083) Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
| """ | ||
| PEP 503 normalization plus dashes as underscores. | ||
| """ | ||
| # Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503 |
There was a problem hiding this comment.
This change removes this important note that links the PEP 503 prescription from the actual implementation.
| # Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503 | ||
| # About 3x faster, safe since packages only support alphanumeric characters | ||
| value = name.translate(_normalize_table) | ||
| # Much faster than re.sub, and even faster than str.translate |
There was a problem hiding this comment.
This comment is now out of place. It's commenting on historical content.
|
These changes also need to be ported to importlib_metadata to avoid being overwritten when syncing that repo. |
Thanks for the review, please see PR python/importlib_metadata#529, and let's fix the comments over there. |



Follow on from #143660 which replaced
re.subwithstr.translate.@henryiii discovered that whilst that is an improvement on Python 3.10-3.11 and 3.14, the performance of
str.translateis much worse on Python 3.12 and 3.13, and can be worse than the originalre.sub.Further, his fix in pypa/packaging#1064 to replace
str.translatewithstr.lowerandstr.replacecalls is better than both the other options across all Python versions, to varying degrees.I benchmarked all three across all available Python Build Standalone versions from 3.10-3.15 on macOS:
I also benchmarked Windows and Ubuntu it's the same pattern.
Whilst the improvement isn't as large for CPython 3.15 as it is for libraries such as
packagingthat support a wide range of Pythons,importlib.metadatadoes have a backport, and I'll update python/importlib_metadata#529 once this is merged.str.translateto improve performance ofimportlib.metadata.Prepared.normalized#143658